package com.xkdx.guangguang.fragment.shop.photo;

import java.util.HashMap;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.Photo;
import com.xkdx.guangguang.shareclass.PhotoAlbum;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class PhotoViewFlipperFragment extends BaseFragment implements OnGestureListener {

	View view;

	ViewFlipper viewFlipper;
	GestureDetector detector;
	Button back, comment;
	int position = 0;
	public int viewFlipperCurrentPosition;
	List<Photo> photoList;
	PhotoAlbum photoAlbum;
	DisplayMetrics metrics;

	String tag;

	int width;

	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.empty_photo).cacheInMemory()
				.cacheOnDisc().build();

		metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
		width = metrics.widthPixels;
		Bundle b = getArguments();
		photoAlbum = (PhotoAlbum) b.getSerializable("photoAlbum");
		photoList = photoAlbum.getList();
		position = b.getInt("position");
		viewFlipperCurrentPosition = position;
		detector = new GestureDetector(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.focuse_image_viewflipper, container, false);
		findView();
		setOnClick();

		return view;
	}

	private void setViewFlipper() {
		setViewFlipperItem(position);

		viewFlipper.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (photoList == null) {
					return false;
				}
				return detector.onTouchEvent(event);
			}
		});
		viewFlipper.setLongClickable(true);

	}

	private void setViewFlipperItem(int i) {
		View photo_view = LayoutInflater.from(getActivity()).inflate(R.layout.photo_view_flipper, null);
		ImageView im = (ImageView) photo_view.findViewById(R.id.photo_view_flipper_im);
		LayoutParams lp = im.getLayoutParams();
		lp.height = width;
		im.setLayoutParams(lp);
		TextView shop_name = (TextView) photo_view.findViewById(R.id.title);
		TextView comment = (TextView) photo_view.findViewById(R.id.commend_number);
		TextView number = (TextView) photo_view.findViewById(R.id.number);
		final ProgressBar pb = (ProgressBar) photo_view.findViewById(R.id.photo_view_flipper_pb);
		TextView shop_user_and_time = (TextView) photo_view.findViewById(R.id.user_and_time);
		imageLoader.displayImage(photoList.get(i).getPhotoUrl(), im, options, new ImageLoadingListener() {

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				pb.setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				pb.setVisibility(View.GONE);

			}

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				pb.setVisibility(View.GONE);

			}

			@Override
			public void onLoadingCancelled(String imageUri, View view) {
				pb.setVisibility(View.GONE);

			}
		});
		shop_name.setText(photoList.get(i).getUserNickName());
		comment.setText(photoList.get(i).getCommentCount());
		if (IConstants.isBrandPhotoComment) {
			if (IConstants.singlePhotoComment.containsKey(photoList.get(i).getPhotoID())) {
				comment.setText(IConstants.singlePhotoComment.get(photoList.get(i).getPhotoID()));
			}
		}

		shop_user_and_time.setText(photoList.get(i).getUserNickName() + " " + photoList.get(i).getCreateTime());
		number.setText((i + 1) + "/" + photoList.size());
		photo_view.setTag(i);
		viewFlipper.addView(photo_view);

	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (e1.getX() - e2.getX() > 120) {
			if (position < photoList.size()-1) {
				setViewFlipperItem(++position);
			} else {
				position = 0;
				setViewFlipperItem(position);
			}
			viewFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.left_in));
			viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.left_out));
			viewFlipper.showNext();
			viewFlipper.removeViewAt(0);
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			if (position > 0) {
				setViewFlipperItem(--position);
			} else {
				position = photoList.size()-1;
				setViewFlipperItem(position);
			}
			viewFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.right_in));
			viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.right_out));
			viewFlipper.showNext();
			viewFlipper.removeViewAt(0);
			return true;
		}
		return false;
	}

	String UserID;
	String LoginToken;
	// 评论点击事件
	OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			SharePrefenceUtil su_user = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
			UserID = su_user.getUserID();
			LoginToken = su_user.getUserLoginToken();
			if ("".equals(LoginToken)) { // 跳转到登陆页面 zhangshun
				UserLogin();
			} else {
				viewFlipperCurrentPosition = (Integer) viewFlipper.getCurrentView().getTag();
				FragmentTransaction ft1 = getFragmentManager().beginTransaction();
				PhotoCommentFragment photoCommentFragment = new PhotoCommentFragment();
				Bundle b = new Bundle();
				b.putString("creatTime", photoList.get(viewFlipperCurrentPosition).getCreateTime());
				b.putString("refreshTime", photoAlbum.getRefreshTime());
				b.putString("photoID", photoList.get(viewFlipperCurrentPosition).getPhotoID());
				b.putString("nickName", photoList.get(viewFlipperCurrentPosition).getUserNickName());
				b.putString("content", photoList.get(viewFlipperCurrentPosition).getPhotoTitle());
				b.putString("picUrl", photoList.get(viewFlipperCurrentPosition).getPhotoUrl());
				b.putString("head", photoList.get(viewFlipperCurrentPosition).getUserAvatars());
				b.putString("count", photoList.get(viewFlipperCurrentPosition).getCommentCount());

				photoCommentFragment.setArguments(b);
				ft1.replace(R.id.fragment, photoCommentFragment);
				ft1.addToBackStack(null);
				ft1.commit();
			}
		}
	};

	@Override
	public void findView() {
		IConstants.hideInputMethodAways(getActivity(), view);
		viewFlipper = (ViewFlipper) view.findViewById(R.id.focuse_ims);
		back = (Button) view.findViewById(R.id.back_focuse_ims);
		comment = (Button) view.findViewById(R.id.photo_comment);
		setViewFlipper();

	}

	@Override
	public void setOnClick() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();
			}
		});
		comment.setOnClickListener(click);

	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {

	}

}
